package com.ruoyi.car.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.car.domain.CarInfo;
import com.ruoyi.car.domain.CarMaintenance;
import com.ruoyi.car.domain.CarMaintenanceRecordTemp;
import com.ruoyi.car.service.ICarInfoService;
import com.ruoyi.car.service.ICarMaintenanceRecordTempService;
import com.ruoyi.car.service.ICarMaintenanceService;
import com.ruoyi.common.utils.DateUtils;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.car.mapper.CarMaintenanceRecordMapper;
import com.ruoyi.car.domain.CarMaintenanceRecord;
import com.ruoyi.car.service.ICarMaintenanceRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 维修保养记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-03
 */
@Service
public class CarMaintenanceRecordServiceImpl implements ICarMaintenanceRecordService {
    @Autowired
    private CarMaintenanceRecordMapper carMaintenanceRecordMapper;

    @Autowired
    private ICarInfoService carInfoService;

    @Autowired
    private ICarMaintenanceRecordTempService carMaintenanceRecordTempService;

    @Autowired
    private ICarMaintenanceService carMaintenanceService;

    /**
     * 查询维修保养记录
     *
     * @param maintenanceRecordId 维修保养记录主键
     * @return 维修保养记录
     */
    @Override
    public CarMaintenanceRecord selectCarMaintenanceRecordByMaintenanceRecordId(Long maintenanceRecordId) {
        CarMaintenanceRecordTemp carMaintenanceRecordTemp = new CarMaintenanceRecordTemp();
        carMaintenanceRecordTemp.setMaintenanceRecordId(maintenanceRecordId);
        List<CarMaintenanceRecordTemp> carMaintenanceRecordTemps = carMaintenanceRecordTempService
                .selectCarMaintenanceRecordTempList(carMaintenanceRecordTemp);
        ArrayList<CarMaintenance> carMaintenances = new ArrayList<>();
        for (CarMaintenanceRecordTemp maintenanceRecordTemp : carMaintenanceRecordTemps) {
            carMaintenances.add(carMaintenanceService.selectCarMaintenanceByMaintenanceId(maintenanceRecordTemp.getMaintenanceId()));
        }
        CarMaintenanceRecord carMaintenanceRecord = carMaintenanceRecordMapper
                .selectCarMaintenanceRecordByMaintenanceRecordId(maintenanceRecordId);
        carMaintenanceRecord.setIdsMaintenance(carMaintenances);
        return carMaintenanceRecord;
    }

    /**
     * 查询维修保养记录列表
     *
     * @param carMaintenanceRecord 维修保养记录
     * @return 维修保养记录
     */
    @Override
    public List<CarMaintenanceRecord> selectCarMaintenanceRecordList(CarMaintenanceRecord carMaintenanceRecord) {
        List<CarMaintenanceRecord> carMaintenanceRecords = carMaintenanceRecordMapper.selectCarMaintenanceRecordList(carMaintenanceRecord);
        for (CarMaintenanceRecord maintenanceRecord : carMaintenanceRecords) {
            List<CarMaintenance> carMaintenances = new ArrayList<>();
            CarMaintenanceRecordTemp carMaintenanceRecordTemp = new CarMaintenanceRecordTemp();
            carMaintenanceRecordTemp.setMaintenanceRecordId(maintenanceRecord.getMaintenanceRecordId());
            List<CarMaintenanceRecordTemp> carMaintenanceRecordTemps = carMaintenanceRecordTempService.selectCarMaintenanceRecordTempList(carMaintenanceRecordTemp);
            for (CarMaintenanceRecordTemp maintenanceRecordTemp : carMaintenanceRecordTemps) {
                CarMaintenance carMaintenance = carMaintenanceService.selectCarMaintenanceByMaintenanceId(maintenanceRecordTemp.getMaintenanceId());
                carMaintenances.add(carMaintenance);
            }
            maintenanceRecord.setIdsMaintenance(carMaintenances);
        }
        return carMaintenanceRecords;
    }

    /**
     * 新增维修保养记录
     *
     * @param carMaintenanceRecord 维修保养记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCarMaintenanceRecord(CarMaintenanceRecord carMaintenanceRecord) {
        //判断车牌
        carInfoService.compare(carMaintenanceRecord.getCarNumber());

        //修改维保状态为进行中
        carMaintenanceRecord.setMaintenanceStatus("0");
        carMaintenanceRecord.setCreateTime(DateUtils.getNowDate());
        int record = carMaintenanceRecordMapper.insertCarMaintenanceRecord(carMaintenanceRecord);

        //增加中间表数据
        extracted(carMaintenanceRecord);
        //修改汽车状态
        CarInfo carInfo = new CarInfo();
        carInfo.setCarNumber(carMaintenanceRecord.getCarNumber());
        carInfo.setType("1");
        carInfoService.updateCarInfoByCarNumber(carInfo);

        return record;
    }

    /**
     * 修改维修保养记录
     *
     * @param carMaintenanceRecord 维修保养记录
     * @return 结果
     */
    @Override
    public int updateCarMaintenanceRecord(CarMaintenanceRecord carMaintenanceRecord) {
        CarInfo carInfo = new CarInfo();
        carInfo.setCarNumber(carMaintenanceRecord.getCarNumber());
        carInfo.setType(carMaintenanceRecord.getMaintenanceStatus());
        carInfoService.updateCarInfoByCarNumber(carInfo);
        carMaintenanceRecord.setUpdateTime(DateUtils.getNowDate());
        int row = carMaintenanceRecordMapper.updateCarMaintenanceRecord(carMaintenanceRecord);

        carMaintenanceRecordTempService.deleteCarMaintenanceRecordTempMaintenanceRecordId(carMaintenanceRecord.getMaintenanceRecordId());
        //增加中间表数据
        extracted(carMaintenanceRecord);
        return row;
    }

    private void extracted(CarMaintenanceRecord carMaintenanceRecord) {
        List<CarMaintenance> carMaintenances = carMaintenanceRecord.getIdsMaintenance();
        if (!Collections.isEmpty(carMaintenances)) {
            Long maintenanceRecordId = carMaintenanceRecord.getMaintenanceRecordId();
            List<CarMaintenanceRecordTemp> carMaintenanceRecordTemps = new ArrayList<>();
            for (CarMaintenance carMaintenancesId : carMaintenances) {
                CarMaintenanceRecordTemp carMaintenanceRecordTemp = new CarMaintenanceRecordTemp();
                carMaintenanceRecordTemp.setMaintenanceId(carMaintenancesId.getMaintenanceId());
                carMaintenanceRecordTemp.setMaintenanceRecordId(maintenanceRecordId);
                carMaintenanceRecordTemps.add(carMaintenanceRecordTemp);
            }
            //批量插入保养项目中间表
            carMaintenanceRecordTempService.batchInsertCarMaintenanceRecordTemp(carMaintenanceRecordTemps);
        }
    }

    /**
     * 批量删除维修保养记录
     *
     * @param maintenanceRecordIds 需要删除的维修保养记录主键
     * @return 结果
     */
    @Override
    public int deleteCarMaintenanceRecordByMaintenanceRecordIds(Long[] maintenanceRecordIds) {
        for (Long maintenanceRecordId : maintenanceRecordIds) {
            carMaintenanceRecordTempService.deleteCarMaintenanceRecordTempMaintenanceRecordId(maintenanceRecordId);
        }
        return carMaintenanceRecordMapper.deleteCarMaintenanceRecordByMaintenanceRecordIds(maintenanceRecordIds);
    }

    /**
     * 删除维修保养记录信息
     *
     * @param maintenanceRecordId 维修保养记录主键
     * @return 结果
     */
    @Override
    public int deleteCarMaintenanceRecordByMaintenanceRecordId(Long maintenanceRecordId) {
        carMaintenanceRecordTempService.deleteCarMaintenanceRecordTempMaintenanceRecordId(maintenanceRecordId);
        return carMaintenanceRecordMapper.deleteCarMaintenanceRecordByMaintenanceRecordId(maintenanceRecordId);
    }
}
